<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card v-loading="isloading">
      <el-button type="primary" @click="showAddCateBox" class="fbtn">添加分类</el-button>
      <tree-table :data="cateList" :columns="columns" :selection-type="false" :expand-type="false" show-index border
        :show-row-hover="true">
        <template #isok='{row}'>
          <i class="el-icon-success ics" v-if="row.cat_delete===false"></i>
          <i class="el-icon-error icr" v-else></i>
        </template>
        <template #operation>
          <el-tooltip effect="light" content="编辑" placement="left">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑 </el-button>
          </el-tooltip>
          <!-- 删除按钮 -->
          <el-tooltip content="删除" placement="top" effect="light">
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
          </el-tooltip>
          <el-button type="warning" size="mini" icon="el-icon-setting">设置
          </el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[2, 3, 10, 15]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- ======================================= -->
    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="40%" @close="restCateForm">
      <!-- 内容主体 -->
      <el-form ref="addCateFormRef" :model="addCateForm" :rules="addCateFormRules" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:" prop="cat_pid">
          <el-cascader clearable v-model="selectKeys" :options="parentCateList" :props="cascaderProps"
            @change="handleChange">
          </el-cascader>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      cascaderProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        checkStrictly: true
        // multiple: true
      },

      selectKeys: [],
      parentCateList: [],
      // 表单验证规则
      addCateFormRules: {
        // 验证规则
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      addCateForm: {
        // 分类名称
        cat_name: '',
        // 添加分类的父级id，0则表示父级为0.添加一级分类
        cat_pid: 0,
        // 添加分类的等级，0则表示添加一级分类
        cat_level: 0
      },
      // 控制对话框的显示与隐藏
      addCateDialogVisible: false,
      // 加载loading
      isloading: false,
      queryInfo: {
        type: 3,
        pagesize: 10, // 每页的显示数据
        pagenum: 1 // 当前页
      },
      // 商品数据分类列表
      cateList: [],
      total: 0,
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok' },
        { label: '操作', type: 'template', template: 'operation' }
      ]
    }
  },
  methods: {
    // bug
    saveCate () {
      this.$refs.addCateFormRef.validate(async bool => {
        if (!bool) return console.log('校验失败')
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加失败')
        this.$message.success('添加分类成功')
        // 成功后关闭窗口
        this.addCateDialogVisible = false
        // 刷新列表
        this.getCateList()
      })
    },
    // 添加完后清空所有表单数据
    restCateForm () {
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_name = ''
      this.selectKeys = []
    },
    handleChange () {
      if (this.selectKeys === 0) {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      } else {
        this.addCateForm.cat_level = this.selectKeys.length
        this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
      }
    },
    // 父级分类数据
    async showAddCateBox () {
      this.addCateDialogVisible = true
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      // this.$message.success('获取成功')
      this.parentCateList = res.data
    },
    // 获取商品列表
    async getCateList () {
      this.isloading = true
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      // this.$message.success('获取成功')
      this.cateList = res.data.result
      this.total = res.data.total
      this.isloading = false
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getCateList()
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getCateList()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.ics {
  color: lightgreen;
  font-size: 20px;
}
.icr {
  color: red;
  font-size: 20px;
}
.fbtn {
  margin-bottom: 20px;
}
</style>
